<template>
  <div class="app-container">
    <div v-if="detailOpen">
      <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
        <el-form-item label="用户姓名" prop="sequenceNumber">
          <el-input
            v-model="nickName"
            placeholder="请输入用户姓名"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="联系电话" prop="sequenceNumber">
          <el-input
            v-model="phonenumber"
            placeholder="请输入联系电话"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>

        <el-form-item label="状态" prop="handleStatus">
          <el-select v-model="queryParams.handleStatus" placeholder="请选择问题状态" clearable size="small">
            <el-option
              v-for="dict in handleStatusOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>

<!--      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="success"
            plain
            icon="el-icon-edit"
            size="mini"
            :disabled="single"
            @click="handleUpdate"
            v-hasPermi="['base:problem:edit']"
          >修改</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="danger"
            plain
            icon="el-icon-delete"
            size="mini"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['base:problem:remove']"
          >删除</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="warning"
            plain
            icon="el-icon-download"
            size="mini"
            :loading="exportLoading"
            @click="handleExport"
            v-hasPermi="['base:problem:export']"
          >导出</el-button>
        </el-col>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>-->

      <el-table v-loading="loading" :data="problemList" @selection-change="handleSelectionChange">
<!--        <el-table-column type="selection" width="55" align="center" />-->
        <el-table-column label="流水号" align="center" prop="sequenceNumber" />
        <el-table-column label="创建时间" align="center" prop="createTime" width="180">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="用户姓名" align="center" prop="lkUser.nickName" />
        <el-table-column label="联系电话" align="center" prop="lkUser.phonenumber" />
        <el-table-column label="用户住址" align="center" prop="lkUser.address" />
        <el-table-column label="问题标题" align="center" prop="problemTitle" />
        <el-table-column label="问题描述" align="center" prop="problemContent" width="400" />
        <el-table-column label="车辆型号" align="center" prop="vehicleModel" />
        <el-table-column label="照片数量" align="center" prop="problemImage" :formatter="handleImageFormat"/>
        <el-table-column label="状态" align="center" prop="handleStatus" :formatter="handleStatusFormat" />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['base:problem:edit']"
            >详情</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['base:problem:remove']"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </div>
    <div v-else>
      <el-row :gutter="20">
        <el-col :span="24" style="padding-bottom: 30px">
          <el-button
            type="primary"
            size="mini"
            icon="el-icon-d-arrow-left"
            @click="cancel"
            plain
          >返回列表</el-button
          >
        </el-col>
        <el-col :sm="24" :lg="12" style="padding-left: 20px">
          <el-card class="box-card" style="height: 730px;overflow-y: auto">
            <div slot="header" class="clearfix">
              <b>问题详情</b>
            </div>
            <el-form ref="form" :model="form" :rules="rules" label-width="100px">
              <el-form-item label="流水单号:" prop="sequenceNumber">
                <b>{{form.sequenceNumber}} </b>
              </el-form-item>
              <el-form-item label="用户姓名:" prop="lkUserId">
                <b>{{form.lkUser.nickName + '    '+form.lkUser.phonenumber}}</b>
              </el-form-item>
              <el-form-item label="车辆型号:" prop="vehicleModel">
                <b>{{form.vehicleModel}}</b>
              </el-form-item>
              <el-form-item label="问题标题:" prop="problemTitle">
                <b>{{form.problemTitle}}</b>
              </el-form-item>
              <el-form-item label="问题描述:" prop="problemContent">
                <b>{{form.problemContent}}</b>
              </el-form-item>
              <el-form-item label="问题图片:" >
                <el-carousel trigger="click" height="360px">
                  <el-carousel-item v-for="item in problemImg" >
                    <img :src="item">
                  </el-carousel-item>
                </el-carousel>
              </el-form-item>
            </el-form>
          </el-card>
        </el-col>
        <el-card class="box-card" style="height: 730px">
          <div slot="header" class="clearfix">
            <b>处理记录</b>
          </div>
          <el-form ref="form" :model="form" :rules="rules" label-width="100px">
            <el-form-item label="核实确认:">
              <el-radio-group v-model="form.confirmStatus">
                <el-radio label="0">已与用户电话联系并确认问题</el-radio>
                <el-radio label="1">未与客户联系确认</el-radio>
              </el-radio-group>
            </el-form-item>
            <el-form-item label="反馈用户:" prop="answerContent">
              <el-input v-model="form.answerContent" type="textarea" :rows="6" placeholder="请输入内容" />
            </el-form-item>
            <el-form-item >
              <el-button type="primary" :disabled="single" @click="answerSubmit">回复用户</el-button>
            </el-form-item>
            <el-form-item label="处理记录:" prop="handleContent">
              <el-input v-model="form.handleContent" type="textarea" :rows="6" placeholder="请输入内容" />
            </el-form-item>
            <el-form-item >
              <el-button type="primary" :disabled="multiple" @click="handleSubmit">处理完成</el-button>
            </el-form-item>
          </el-form>
        </el-card>
      </el-row>
    </div>
  </div>
</template>

<script>
import {
  listProblem,
  getProblem,
  delProblem,
  addProblem,
  updateProblem,
  exportProblem,
  answerProblem, handleProblem
} from "@/api/base/problem";
import ImageUpload from '@/components/ImageUpload';

export default {
  name: "Problem",
  components: {
    ImageUpload,
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedLkUser: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 问题反馈表格数据
      problemList: [],
      problemImg:[],
      // 弹出层标题
      title: "",
      detailOpen:true,
      // 问题状态 0待处理 1已确认 2已处理字典
      handleStatusOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        sequenceNumber: null,
        handleStatus:null
      },
      nickName:"",
      phonenumber:"",
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("handle_status").then(response => {
      this.handleStatusOptions = response.data;
    });
  },
  methods: {
    /** 查询问题反馈列表 */
    getList() {
      this.loading = true;
      this.queryParams.params={}
      this.queryParams.params["nickName"] = this.nickName;
      this.queryParams.params["phonenumber"] = this.phonenumber;
      listProblem(this.queryParams).then(response => {
        this.problemList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 问题状态 0待处理 1已确认 2已处理字典翻译
    handleStatusFormat(row, column) {
      return this.selectDictLabel(this.handleStatusOptions, row.handleStatus);
    },
    handleImageFormat(row, column) {
      if(row.problemImage!='' && row.problemImage!=null ){
        let img=row.problemImage.split(",");
        return img.length
      }
      return 0;
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.detailOpen=true;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        sequenceNumber: null,
        createTime: null,
        lkUserId: null,
        problemTitle: null,
        problemContent: null,
        vehicleModel: null,
        problemImage: null,
        handleStatus: null,
        confirmStatus: "0",
        answerContent: null,
        answerTime: null,
        handleContent: null,
        handleTime: null,
        userId: null
      };
      this.lkUserList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.nickName="";
      this.phonenumber="";
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      /*this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length*/
    },
    /** 新增按钮操作 */
    /*handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加问题反馈";
    },*/
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getProblem(id).then(response => {
        this.problemImg = [];
        this.form = response.data;
        if(this.form.problemImage!=[]){
          let img=this.form.problemImage.split(",");
          for (let i = 0; i < img.length; i++) {
            this.problemImg.push(img[i])
          }
        }
        /*for(let k=0;k<info.length;k++){
          var img = {};
          img.id=info[k].noticeId;
          img.title = info[k].noticeTitle;
          img.idView = process.env.VUE_APP_BASE_API+info[k].imgUrl;
          this.newsImg.push(img);
        }*/
        if (this.form.handleStatus =='0'){
          this.single=false;
        }else if(this.form.handleStatus=='1'){
          this.multiple=false;
        }

        this.detailOpen = false;
        this.title = "问题详情";
      });
    },
    /** 提交按钮 */
    answerSubmit() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            answerProblem(this.form).then(response => {
              this.msgSuccess("回复成功");
              this.single=true
              this.getList();
            });
          }
        }
      });
    },
    handleSubmit() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            handleProblem(this.form).then(response => {
              this.msgSuccess("处理成功");
              this.multiple=true;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除问题反馈编号为"' + ids + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function() {
        return delProblem(ids);
      }).then(() => {
        this.getList();
        this.msgSuccess("删除成功");
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有问题反馈数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        this.exportLoading = true;
        return exportProblem(queryParams);
      }).then(response => {
        this.download(response.msg);
        this.exportLoading = false;
      })
    }
  }
};
</script>

<style>
.el-carousel__item h3 {
  color: #475669;
  font-size: 14px;
  opacity: 0.75;
  line-height: 150px;
  margin: 0;
}

.el-carousel__item:nth-child(2n) {
  background-color: #99a9bf;
}

.el-carousel__item:nth-child(2n+1) {
  background-color: #d3dce6;
}
</style>
